package com.example.btmac;

import java.util.List;

import com.example.btmac.R;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class BlueAdapter3 extends BaseAdapter {
	
	private Context mContext;
	private List<BluetoothDevice> mList;

	public BlueAdapter3(Context context,List<BluetoothDevice> list){
		this.mContext = context;
		this.mList = list;
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup group) {
		ViewHolder holder;
		
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(mContext).inflate(R.layout.item, null);
			holder.name = (TextView) convertView.findViewById(R.id.item_name);
			holder.mac = (TextView) convertView.findViewById(R.id.item_mac);
			holder.type = (TextView) convertView.findViewById(R.id.item_type);
			convertView.setTag(holder);
		}else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		BluetoothDevice blueDevice = mList.get(position);
        final String deviceName = blueDevice.getName();
        //名称
        if (deviceName != null && deviceName.length() > 0) {
            holder.name.setText(blueDevice.getName());
        } else {
        	holder.name.setText("未知设备");
        }
        //mac
		holder.mac.setText(mList.get(position).getAddress());
		//设备类型
		int deviceClass = blueDevice.getBluetoothClass().getDeviceClass();
		String type = getDeviceType(deviceClass);
		if (!TextUtils.isEmpty(type)) {
			holder.type.setText(type);
		}else {
			holder.type.setText(deviceClass+"");
		}
		
		return convertView;
	}
	
	static class ViewHolder{
		TextView name;
		TextView mac;
		TextView type;
	}
	
	private String getDeviceType(int code) {
		String type = "";
		switch (code) {
		case 268:
			type = "电脑";
			break;
		case 524:
			type = "手机";
			break;
		case 1032:
			type = "耳机";
			break;
		case 1084:
			type = "TV";
			break;
		case 7936:
			type = "未知";
			break;
		}

		return type;
	}
}
